package com.example.studentalarm.activity;


import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.studentalarm.R;
import com.example.studentalarm.bean.AlarmConfigBean;
import com.example.studentalarm.config.SharedPreferencesUtils;
import com.example.studentalarm.presenter.SetMvpPresenter;
import com.example.studentalarm.utils.Utils;
import com.example.studentalarm.view.ISetView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/***
 * 设置activity
 */
public class SetActivity extends BaseModelActivity<ISetView, SetMvpPresenter> implements ISetView {
    @BindView(R.id.tvGoBack)
    TextView tvGoBack;
    @BindView(R.id.cb_music)
    CheckBox cbMusic;
    @BindView(R.id.cb_vibration)
    CheckBox cbVibration;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    private Context mContext;

    @Override
    public int getLayoutUrl() {
        return R.layout.activity_set;
    }

    @Override
    public void registerById() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        mContext = this;
        presenter.attach(this);
        tvVersion.setText(new StringBuffer("V").append(Utils.getVersionName(mContext)).toString());
        presenter.requestConfigBean();
    }

    @Override
    protected SetMvpPresenter initPresenter() {
        return new SetMvpPresenter(this);
    }


    @OnClick({R.id.tvGoBack,  R.id.cb_music, R.id.cb_vibration})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvGoBack:
                finishActivity();
                break;
            case R.id.cb_music:
                if (!cbMusic.isChecked() && !cbVibration.isChecked()) {
                    Utils.toast(mContext, "至少开启一个铃声或振动！");
                    cbMusic.setChecked(true);
                    return;
                }
                SharedPreferencesUtils.getInstance(this).saveOpenMusic(cbMusic.isChecked());
                break;
            case R.id.cb_vibration:
                if (!cbVibration.isChecked() && !cbMusic.isChecked()) {
                    Utils.toast(mContext, "至少开启一个铃声或振动！");
                    cbVibration.setChecked(true);
                    return;
                }
                SharedPreferencesUtils.getInstance(this).saveOpenVibration(cbVibration.isChecked());
                break;
        }
    }

    @Override
    public void getAlarmConfigBean(AlarmConfigBean alarmConfigBean) {
        cbMusic.setChecked(alarmConfigBean.isMusic());
        cbVibration.setChecked(alarmConfigBean.isVibration());
    }

}
